﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ConferenceRegister.Domain;
using ConferenceRegister.Models;
using System.Configuration;
using ConferenceRegister.Mailers;
using System.Threading.Tasks;

namespace ConferenceRegister.Controllers
{
    public class HomeController : Controller
    {
        RegisterService service = new RegisterService();
        UserMailer Mailer = new UserMailer(System.Web.HttpContext.Current);

        private ConferenceModel ConferenceModel(Conference c, Register r=null)
        {
            List<vSeat> seats = c == null ? null : service.Seats(c.ID);
            vSeat s = c == null ? null : service.AvailableSeat(c.ID);
            return new ConferenceModel()
            {
                Seats = seats,
                SeatName = s == null ? "" : s.Name,
                Ministries = service.AvailableMinistries(),
                SeatID = s == null ? 0 : s.ID,
                Conference = c,
                Register = r,
                IsPrivateClosed = c == null ? true : service.IsPrivateRegistrationClosed(c.ID),
                IsPublicClosed = c == null ? true : service.IsPublicRegistrationClosed(c.ID)
            };
        }

        public ActionResult Index(int id=0)
        {
            Conference c;
            if (id == 0)
            {
                c = service.LatestConference();
                id = c.ID;
            }
            else 
                c = service.All.Conferences.Find(id);
            Session["ConferenceID"] = c==null ? null : (int?)id;

            return View(ConferenceModel(c));
        }

        [HttpPost]
        public ActionResult Index(ConferenceModel model)
        {
            if (ModelState.IsValid)
            {
                Register r = model.Register;
                r.ConferenceID = model.Conference.ID;
                vSeat seat = service.AvailableSeat(conferenceID:model.Conference.ID);
                if (seat != null) 
                    r.SeatID = seat.ID;
                Ministry ministry = model.MinistryID == null ? null : service.All.Ministries.Find(model.MinistryID);
                MinistrySignup slot = ministry == null ? null : service.AvailableMinistrySlot(ministry.ID);

                if (seat == null)
                {
                    ModelState.AddModelError("SeatID", "We apologize, but the conference is full");
                }
                else if (ministry != null && slot == null)
                {
                    ModelState.AddModelError("MinistryID", "The " + ministry.Name + " ministry is full. Please select another.");
                }
                else if (service.IsDuplicate(model.Register))
                    ModelState.AddModelError("", "Duplicate entry found. If registering for a family member, please provide an accurate first name.");
                else
                {
                    r.MinistrySignup = slot;
                    r.SubmitDate = DateTime.Now;
                    r.FixPhoneNumbers();
                    service.All.Registrants.Create(r);
                    return RedirectToAction("Confirm", new { id = r.ID });
                }
            }

            return View(ConferenceModel(service.All.Conferences.Find(model.Conference.ID), model.Register));
        }

        public ActionResult Contact()
        {
            return View();
        }

        public ActionResult Confirm(int id)
        {
            Register r = service.All.Registrants.Find(id);
            Mailer.ConfirmRegistration(r);
            return View(r);
        }
    }
}
